package com.it.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.it.entity.User;
import com.it.mapper.UserMapper;
import com.it.service.UserService;
import com.it.util.ItdragonUtils;
import com.it.util.Result;
import com.it.util.ResultResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户service实现类
 *
 * @author
 */
@Service
public class UserServiceImpl implements UserService {
    private static final String ROLE_ID = "035e6cd6738c42e5a4112d34e85e0832";//初始用户角色Id
    private static final String USER_IMAGE = "/image/default.jpg";//初始用户默认的头像
    private static final String Type = "用户";//初始用户默认的头像

    @Resource
    private UserMapper userMapper;

    @Resource
    private ItdragonUtils itdragonUtils;

    @Override
    public void sendMsg() {
    }



    @Override
    public User getUserByUserPhoneNum(String phoneNum) {
        EntityWrapper<User> userEntityWrapper = new EntityWrapper<>();
        Wrapper<User> phoneNum1 = userEntityWrapper.eq("phoneNum", phoneNum);
        List<User> users = userMapper.selectList(userEntityWrapper);
        if (users.size() > 0) {
            User user = users.get(0);
            return user;
        }
        return null;
    }


    @Override
    public User getMyInformation(User user) {
        String id = user.getId();
        User users = userMapper.selectMyInformation(id);
        return users;
    }

    @Override
    public ResultResponse updateUserIntegral(User user) {
        int integral = user.getIntegral();
        int i = --integral;
        if (i < 0) {
            return Result.resuleError("你的剩余积分不足");
        }
        user.setIntegral(i);
        Integer integer = userMapper.updateById(user);
        if (integer > 0) {
            return Result.resuleSuccess();
        }
        return Result.resuleError();
    }

    @Override
    public User getUserByUserName(String userName) {
        User searchUser = new User();
        searchUser.setUserName(userName);
        User user = userMapper.selectOne(searchUser);
        if (user != null) {

            return user;
        }
        return null;
    }





    @Override
    public boolean insert(User user) {
        user.setImgUrl(USER_IMAGE);
        //判断前台是否传入用户角色
        if (!ItdragonUtils.stringIsNotBlack(user.getRoleId())) {
            user.setRoleId(ROLE_ID);
        }
        if (!ItdragonUtils.stringIsNotBlack(user.getType())) {
            user.setType(Type);
        }
        if (ItdragonUtils.isEncrypted()) {
            itdragonUtils.entryptPassword(user);
        } else {
            user.setPassword(user.getPassword());
        }
        int result = userMapper.insert(user);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void updateLoginTime(User user, String time) {
        String phoneNum = user.getPhoneNum();
        EntityWrapper<User> entityWrapper = new EntityWrapper<User>();
        user.setUpdatedDate(time);
        entityWrapper.eq("phoneNum", phoneNum);
        userMapper.update(user, entityWrapper);
    }









    @Override
    public boolean deleteByPrimaryKey(String ids) {
        String[] idList = ids.split(",");
        int result = 0;
        for (String s : idList) {
            result = userMapper.deleteById(s);
        }
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public User selectByPrimaryKey(String id) {
        User user = userMapper.selectById(id);
        if (user != null) {
            return user;
        }
        return null;
    }

    @Transactional
    @Override
    public boolean updateByPrimaryKey(User user) {
        if (ItdragonUtils.isEncrypted()) {
            itdragonUtils.entryptPassword(user);
        } else {
            user.setPassword(user.getPlainPassword());
        }
        int result = userMapper.updateById(user);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }
}